<?php

class core_jobs extends object {

public $id="";
public $uid="";
public $jid="";
public $jdesc="";
public $from="";
public $to="";
public $duration="";
public $login="";
public $note="";
public $admin=0;

}

class core_job extends core_jobs {

function var_validate() {
	$out="";	
	$loc = new loc;
	if ($this->uid == "") {
		$out=$out."<br>".$loc->addjob_missing_uid; 		
		}	
	if ($this->jid == "") {
		$out=$out."<br>".$loc->addjob_missing_jid; 		
		}	
	if ($this->from == "") {
		$out=$out."<br>".$loc->addjob_missing_start; 		
		}	
	if ($this->to == "") {
		$out=$out."<br>".$loc->addjob_missing_end; 		
		}
	if ($this->duration == "") {
		$out=$out."<br>".$loc->addjob_missing_duration; 		
		}
	if ($this->login == "") {
		$out=$out."<br>".$loc->addjob_missing_login; 		
		}
	return $out;
	}


function returnjobpath_old() {
	$path="";	
	$query="select name from tree where left_state <= (select left_state from tree where ID = ".mysql_real_escape_string($this->jid).") AND right_state >= (select right_state from tree where ID = ".mysql_real_escape_string($this->jid).")";
	$result=mysql_query($query);
	while($row=mysql_fetch_array($result)) {
		$path=$path." -> ".$row['name'];		
		}	
	return $path;
	}
	
function returnjobpath() {
	$path="";	
	$counter=0;
	$query="select name from tree where left_state <= (select left_state from tree where ID = ".mysql_real_escape_string($this->jid).") AND right_state >= (select right_state from tree where ID = ".mysql_real_escape_string($this->jid).") ORDER BY LEVEL";
	$result=mysql_query($query);
	while($row=mysql_fetch_array($result)) {
		if ($counter==0) {
				$path=$row['name']; //first in row
				} else {
				$path=$path.">".$row['name'];				
				}
				$counter++;		
		}
	//return $query;	
	return $path;
	}

//overuje, ci sa dany job neprekryva s niecim inym
function add_validate() {
	$out="";	
	$loc = new loc;
	$time = new core_time;
	$validation=$this->var_validate();	
	if ($validation == "") {
		$query="select * from userjobs where uid = ".mysql_real_escape_string($this->uid)." AND from_ < ".mysql_real_escape_string($this->to)." AND to_ >  ".mysql_real_escape_string($this->from)."";
		$result=mysql_query($query);
		if ($result != "") {
			while($row=mysql_fetch_array($result)) {
				if ($this->id != $row['id']){
					$out=$out."<br>".$loc->addjob_validate_overlap." ".$row['jdesc']." ".$time->totimedef($row['from_'])." - ".$time->totimedef($row['to_']);
					} 
				}
			}
		if ($this->to <= $this->from) {$out=$out."<br>".$loc->addjob_validate_stoplessthanstart;}	
		if (($this->to - $this->from) != $this->duration) {
			$out=$out."<br>".$loc->addjob_validate_durationfit;			
			}	
		} else {$out=$out."<br>".$validation;}
	return $out; 	
	}

function add() {
	$validation=$this->add_validate();
	if ($validation == "") {
		$this->jdesc=$this->returnjobpath();
		$query="insert into userjobs (uid,jid,jdesc,from_,to_,duration,login,note) values (".mysql_real_escape_string($this->uid).",".mysql_real_escape_string($this->jid).",'".mysql_real_escape_string($this->jdesc)."',".mysql_real_escape_string($this->from).",".mysql_real_escape_string($this->to).",".mysql_real_escape_string($this->duration).",'".mysql_real_escape_string($this->login)."','".mysql_real_escape_string($this->note)."')";		
		mysql_query($query);
		//return $out;		
		}	
	}
	
//podla id naloaduje do premennych hodnoty
function setbyID($id) {
	$query="select * from userjobs where id = ".mysql_real_escape_string($id)."";
	$result=mysql_query($query);
	if($result != "") {$row=mysql_fetch_array($result);}	
	if ($this->uid == "") {
		$this->uid=$row['uid']; 		
		}	
	if ($this->jid == "") {
		$this->jid=$row['jid']; 		
		}	
	if ($this->from == "") {
		$this->from=$row['from_']; 		
		}	
	if ($this->to == "") {
		$this->to=$row['to_']; 		
		}
	if ($this->duration == "") {
		$this->duration=$row['duration']; 		
		}
	if ($this->login == "") {
		$this->login=$row['login']; 		
		}
	if ($this->note == "") {
		$this->note=$row['note']; 		
		}
	$this->id=$id;
	$this->jdesc=$this->returnjobpath();
	}

function edit_validate() {
	if ($this->id != "") {
		$this->setbyID($this->id);
		$validation=$this->add_validate();
		}
	return $validation;
	}
	
function edit() {
		$validation=$this->edit_validate();
		if ($validation == "") {
			$query="update userjobs set uid = ".mysql_real_escape_string($this->uid).",jid = ".mysql_real_escape_string($this->jid).",jdesc = '".mysql_real_escape_string($this->jdesc)."',from_ = ".mysql_real_escape_string($this->from).",to_ = ".mysql_real_escape_string($this->to).",duration = ".mysql_real_escape_string($this->duration).",login = '".mysql_real_escape_string($this->login)."',note = '".mysql_real_escape_string($this->note)."' where id=".mysql_real_escape_string($this->id)."";
			mysql_query($query);			
			}
		}

function delete() {
	if ($this->id != "" AND $this->uid != "") {
		$query="delete from userjobs where id=".mysql_real_escape_string($this->id)." AND uid=".mysql_real_escape_string($this->uid)."";
		mysql_query($query);	
		}
		return $query;
	}		

}

?>
